package com.common.www.controller.project;

import org.springframework.boot.base.jpa.service.IBaseService;
import com.common.www.bean.entity.Result;
import com.common.www.bean.entity.programme.TabulationExtracting;
import com.common.www.bean.entity.project.BiddingCompany;
import com.common.www.bean.entity.tabulation.*;
import com.common.www.bean.vo.StatisticalBiddingVo;
import com.common.www.controller.base.DefaultCrudController;
import com.common.www.data.NetCode;
import com.common.www.service.programme.TabulationExtractingService;
import com.common.www.service.project.BiddingCompanyService;
import com.common.www.service.tabulation.*;
import com.common.www.utils.ParamsUtils;
import com.common.www.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;


@CrossOrigin
@RestController
@RequestMapping(value = {"mgt/biddingCompany"})
@Api(tags = "MGT 高低压配电绑定供应商管理接口")
public class MGTBiddingCompanyController extends DefaultCrudController<BiddingCompany, Long> {
    @Autowired
    private BiddingCompanyService service;
    @Autowired
    private InnerBidBaseTabulationService innerBidBaseTabulationService;
    @Autowired
    private OuterBidBaseTabulationService outerBidBaseTabulationService;
    @Autowired
    private InnerTabulationService innerTabulationService;
    @Autowired
    private OuterTabulationService outerTabulationService;
    @Autowired
    private TabulationExtractingService tabulationExtractingService;


    protected IBaseService getService() {
        return service;
    }


    @ApiOperation("批量添加项目供应商")
    @RequestMapping(value = "/saveByJsonBatch", method = RequestMethod.POST)
    public Result saveByJsonBatch(@RequestBody List<BiddingCompany> biddingCompanys) {
        for (BiddingCompany biddingCompany : biddingCompanys) {
            if (biddingCompany.getId() != null) {
                // 更新操作
                BiddingCompany existing = service.getByIdLogic(biddingCompany.getId());
                if (existing == null) {
                    throw new ServiceException("未找到目标对象");
                }
                // 复制属性到现有对象，排除ID字段
                BeanUtils.copyProperties(biddingCompany, existing, "id");
                // 保存现有对象
                service.save(existing);
            } else {
                // 新增操作，直接保存传入的对象
                service.save(biddingCompany);
            }
        }
        return R.ok(biddingCompanys);
    }

    @ApiOperation("编辑项目供应商状态")
    @RequestMapping(value = "/saveByJson", method = RequestMethod.POST)
    public Result saveByJson(@RequestBody BiddingCompany biddingCompany) {
        if (biddingCompany.getId() != null) {
            // 更新操作
            BiddingCompany existing = service.getByIdLogic(biddingCompany.getId());
            if (existing == null) {
                throw new ServiceException("未找到目标对象");
            }
            // 复制属性到现有对象，排除ID字段
            BeanUtils.copyProperties(biddingCompany, existing, "id");
            // 保存现有对象
            service.save(existing);
            return R.ok(existing);
        } else {
            // 新增操作，直接保存传入的对象
            service.save(biddingCompany);
            return R.ok(biddingCompany);
        }
    }


    @ApiOperation("批量删除启用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "String", defaultValue = ""),
    })
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    public Result batchDelete(Long[] ids) {
        if (ParamsUtils.isInputEmpty(ids)) {
            return R.returnCode(NetCode.CODE_INPUT_OMIT);
        }
        for (int i = 0; i < ids.length; i++) {
            BiddingCompany biddingCompany = service.findOne(ids[i]);
            if (biddingCompany != null) {
                biddingCompany.setFlag(0);
                service.save(biddingCompany);
                //删除子信息
                List<InnerTabulation> innerTabulations = innerTabulationService.getRepository().findByFlagAndBiddingCompanyId(1,ids[i]);
                List<OuterTabulation> outerTabulations = outerTabulationService.getRepository().findByFlagAndBiddingCompanyId(1,ids[i]);
                List<TabulationExtracting> tabulationExtractings = tabulationExtractingService.getRepository()
                        .findByFlagAndProjectIdAndSupplyCompanyIdAndInnerOuterType(1,biddingCompany.getProject().getId(),biddingCompany.getSupplyCompany().getId(),biddingCompany.getInnerOuterType());
                if (innerTabulations != null && innerTabulations.size() > 0) {
                    innerTabulationService.delete(innerTabulations);
                }
                if (outerTabulations != null && outerTabulations.size() > 0) {
                    outerTabulationService.delete(outerTabulations);
                }
                if (tabulationExtractings != null && tabulationExtractings.size() > 0) {
                    tabulationExtractingService.delete(tabulationExtractings);
                }
            }
        }
        return R.ok();
    }



    @ApiOperation(value = "查询清单信息(供应商等组合信息)", nickname = "", notes = "")
    @ApiImplicitParams(value = {
    })
    @RequestMapping(value = "/query2", produces = {"application/json"}, method = RequestMethod.POST)
    public Result query2(Long projectId, Integer innerOuterType) {
        HashMap hashMap = new HashMap();
        hashMap.put("project.id", projectId);
        hashMap.put("innerOuterType", innerOuterType);
        List<BiddingCompany> companyList = service.getListLogic(hashMap, null);

        //查询内外标底(标底、实施标底)
        List<InnerBidBaseTabulation> innerBidBaseTabulations1 = innerBidBaseTabulationService.getRepository().findByFlagAndProjectIdAndBidType(1, projectId, 1);
        List<InnerBidBaseTabulation> innerBidBaseTabulations2 = innerBidBaseTabulationService.getRepository().findByFlagAndProjectIdAndBidType(1, projectId, 2);
        List<OuterBidBaseTabulation> outerBidBaseTabulations1 = outerBidBaseTabulationService.getRepository().findByFlagAndProjectIdAndBidType(1, projectId, 1);
        List<OuterBidBaseTabulation> outerBidBaseTabulations2 = outerBidBaseTabulationService.getRepository().findByFlagAndProjectIdAndBidType(1, projectId, 2);
        Map<String, Object> mapInnerBidBase1 = innerBidBaseTabulationService.getInformation(innerBidBaseTabulations1, 1);
        Map<String, Object> mapInnerBidBase2 = innerBidBaseTabulationService.getInformation(innerBidBaseTabulations2, 2);
        Map<String, Object> mapOuterBidBase1 = outerBidBaseTabulationService.getInformation(outerBidBaseTabulations1, 1);
        Map<String, Object> mapOuterBidBase2 = outerBidBaseTabulationService.getInformation(outerBidBaseTabulations2, 2);


        BiddingCompany item = null;
        if (companyList.size() == 0) {
            if (innerBidBaseTabulations1.size() > 0 || innerBidBaseTabulations2.size() > 0
                    || outerBidBaseTabulations1.size() > 0 || outerBidBaseTabulations2.size() > 0){
                item = new BiddingCompany();
                item.setInnerBidBaseTabulation1(innerBidBaseTabulations1.size() > 0 ? mapInnerBidBase1 : null);
                item.setInnerBidBaseTabulation2(innerBidBaseTabulations2.size() > 0 ? mapInnerBidBase2 : null);
                item.setOuterBidBaseTabulation1(outerBidBaseTabulations1.size() > 0 ? mapOuterBidBase1 : null);
                item.setOuterBidBaseTabulation2(outerBidBaseTabulations2.size() > 0 ? mapOuterBidBase2 : null);
                companyList.add(item);
            }
        } else {
            for (BiddingCompany company : companyList) {
                company.setInnerBidBaseTabulation1(innerBidBaseTabulations1.size() > 0 ? mapInnerBidBase1 : null);
                company.setInnerBidBaseTabulation2(innerBidBaseTabulations2.size() > 0 ? mapInnerBidBase2 : null);
                company.setOuterBidBaseTabulation1(outerBidBaseTabulations1.size() > 0 ? mapOuterBidBase1 : null);
                company.setOuterBidBaseTabulation2(outerBidBaseTabulations2.size() > 0 ? mapOuterBidBase2 : null);

                //查询内外回标清单(回标1、回标2、...)
                List<InnerTabulation> innerTabulations1 = innerTabulationService.getRepository().findByFlagAndBiddingCompanyIdAndJieDuan(1, company.getId(), 2);
                List<InnerTabulation> innerTabulations2 = innerTabulationService.getRepository().findByFlagAndBiddingCompanyIdAndJieDuan(1, company.getId(), 3);
                List<InnerTabulation> innerTabulations3 = innerTabulationService.getRepository().findByFlagAndBiddingCompanyIdAndJieDuan(1, company.getId(), 4);
                List<InnerTabulation> innerTabulations4 = innerTabulationService.getRepository().findByFlagAndBiddingCompanyIdAndJieDuan(1, company.getId(), 5);
                List<InnerTabulation> innerTabulations5 = innerTabulationService.getRepository().findByFlagAndBiddingCompanyIdAndJieDuan(1, company.getId(), 6);
                List<InnerTabulation> innerTabulations6 = innerTabulationService.getRepository().findByFlagAndBiddingCompanyIdAndJieDuan(1, company.getId(), 8);

                List<OuterTabulation> outerTabulations1 = outerTabulationService.getRepository().findByFlagAndBiddingCompanyIdAndJieDuan(1, company.getId(), 2);
                List<OuterTabulation> outerTabulations2 = outerTabulationService.getRepository().findByFlagAndBiddingCompanyIdAndJieDuan(1, company.getId(), 3);
                List<OuterTabulation> outerTabulations3 = outerTabulationService.getRepository().findByFlagAndBiddingCompanyIdAndJieDuan(1, company.getId(), 4);
                List<OuterTabulation> outerTabulations4 = outerTabulationService.getRepository().findByFlagAndBiddingCompanyIdAndJieDuan(1, company.getId(), 5);
                List<OuterTabulation> outerTabulations5 = outerTabulationService.getRepository().findByFlagAndBiddingCompanyIdAndJieDuan(1, company.getId(), 6);
                List<OuterTabulation> outerTabulations6 = outerTabulationService.getRepository().findByFlagAndBiddingCompanyIdAndJieDuan(1, company.getId(), 8);

                StatisticalBiddingVo mapInnerT1 = innerTabulationService.getInformation(innerTabulations1, 2);
                StatisticalBiddingVo mapInnerT2 = innerTabulationService.getInformation(innerTabulations2, 3);
                StatisticalBiddingVo mapInnerT3 = innerTabulationService.getInformation(innerTabulations3, 4);
                StatisticalBiddingVo mapInnerT4 = innerTabulationService.getInformation(innerTabulations4, 5);
                StatisticalBiddingVo mapInnerT5 = innerTabulationService.getInformation(innerTabulations5, 6);
                StatisticalBiddingVo mapInnerT6 = innerTabulationService.getInformation(innerTabulations6, 8);

                StatisticalBiddingVo mapOuterT1 = outerTabulationService.getInformation(outerTabulations1, 2);
                StatisticalBiddingVo mapOuterT2 = outerTabulationService.getInformation(outerTabulations2, 3);
                StatisticalBiddingVo mapOuterT3 = outerTabulationService.getInformation(outerTabulations3, 4);
                StatisticalBiddingVo mapOuterT4 = outerTabulationService.getInformation(outerTabulations4, 5);
                StatisticalBiddingVo mapOuterT5 = outerTabulationService.getInformation(outerTabulations5, 6);
                StatisticalBiddingVo mapOuterT6 = outerTabulationService.getInformation(outerTabulations6, 8);

                company.setInnerTabulation1(innerTabulations1.size() > 0 ? mapInnerT1 : null);
                company.setInnerTabulation2(innerTabulations2.size() > 0 ? mapInnerT2 : null);
                company.setInnerTabulation3(innerTabulations3.size() > 0 ? mapInnerT3 : null);
                company.setInnerTabulation4(innerTabulations4.size() > 0 ? mapInnerT4 : null);
                company.setInnerTabulation5(innerTabulations5.size() > 0 ? mapInnerT5 : null);
                company.setInnerTabulation6(innerTabulations6.size() > 0 ? mapInnerT6 : null);
                company.setOuterTabulation1(outerTabulations1.size() > 0 ? mapOuterT1 : null);
                company.setOuterTabulation2(outerTabulations2.size() > 0 ? mapOuterT2 : null);
                company.setOuterTabulation3(outerTabulations3.size() > 0 ? mapOuterT3 : null);
                company.setOuterTabulation4(outerTabulations4.size() > 0 ? mapOuterT4 : null);
                company.setOuterTabulation5(outerTabulations5.size() > 0 ? mapOuterT5 : null);
                company.setOuterTabulation6(outerTabulations6.size() > 0 ? mapOuterT6 : null);
            }
        }
        List<BiddingCompany> sortedList = companyList.stream()
                .filter(vo -> vo.getBidStatus() != null)
                .sorted(Comparator.comparingInt(BiddingCompany::getBidStatus))
                .collect(Collectors.toList());
        return R.ok(sortedList);
    }


}

